package com.pi.pro.xcmg.am.was.fac.handler;

import com.pi.framework.core.util.StringUtils;
import com.pi.wms.common.WmsTextUtils;
import com.pi.wms.entity.TwaDocAsnHeader;
import com.pi.wms.entity.TwaDocCoHeader;
import com.pi.wms.entity.TwaDocSoDetails;
import com.pi.wms.entity.TwmBasMaterial;
import com.pi.wms.fac.CustomPredicateQuery;

import javax.persistence.criteria.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class CoCustomPredicate extends CustomPredicateQuery<TwaDocCoHeader> {

    @Override
    public void toPredicate(Root<TwaDocCoHeader> root, List<Predicate> predicates, CriteriaQuery<?> criteriaQuery, CriteriaBuilder builder, Map<String, ?> query) {
        if (!this.validate(root, predicates, criteriaQuery, builder, query)){
            return;
        }
        Join<TwaDocCoHeader, TwaDocSoDetails> lineJoin=null;
        //状态
        String materialcode = (String) query.get("materialcode");
        if (!StringUtils.isNullOrBlank(materialcode)) {
            if (lineJoin==null){
                lineJoin = root.join("details");
            }
            predicates.add(builder.like(lineJoin.get("materialCode"), "%" + materialcode.trim() + "%"));
        }
        String materialname = (String) query.get("materialname");
        if (!StringUtils.isNullOrBlank(materialname)) {
            if (lineJoin==null){
                lineJoin = root.join("details");
            }
            predicates.add(builder.like(lineJoin.get("materialName"), "%" + materialname.trim() + "%"));
        }
        String lotattr06 = (String) query.get("lotattr06");
        if (!StringUtils.isNullOrBlank(lotattr06)) {
            if (lineJoin==null){
                lineJoin = root.join("details");
            }
            predicates.add(builder.like(lineJoin.get("fromLotattr06"), "%" + lotattr06.trim() + "%"));
        }
        String lotattr07 = (String) query.get("lotattr07");
        if (!StringUtils.isNullOrBlank(lotattr07)) {
            if (lineJoin==null){
                lineJoin = root.join("details");
            }
            predicates.add(builder.like(lineJoin.get("fromLotattr07"), "%" + lotattr07.trim() + "%"));
        }
        String containnum = (String) query.get("containnum");
        if (!StringUtils.isNullOrBlank(containnum)) {
            if (lineJoin==null){
                lineJoin = root.join("details");
            }
            predicates.add(builder.like(lineJoin.get("containNumber"), "%" + containnum.trim() + "%"));
        }

        Date date = null;
        String fromdate = (String)query.get("fromdate");
        if(!StringUtils.isNullOrBlank(fromdate)) {
            date = WmsTextUtils.plainDate(fromdate, true);
            if(date != null) {
                predicates.add(builder.greaterThanOrEqualTo(root.get("createdDate"), date));
            }
        }
        String todate = (String)query.get("todate");
        if(!StringUtils.isNullOrBlank(todate)) {
            date = WmsTextUtils.plainDate(todate, false);
            if(date != null) {
                predicates.add(builder.lessThanOrEqualTo(root.get("createdDate"), date));
            }
        }




    }
}
